package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//菜品管理
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
  @PostMapping("")
  @ApiOperation(value = "新增菜品")
   public Result save(@RequestBody DishDTO dishDTO){
       log.info("新增菜品：{}", dishDTO);
       dishService.saveWithFlavor(dishDTO);
       return Result.success();
   }
   @GetMapping("/page")
    @ApiOperation(value = "分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
       log.info("分页查询菜品：{}", dishPageQueryDTO);
       PageResult pageResult = dishService.page1(dishPageQueryDTO);
       return Result.success(pageResult);
   }

   @DeleteMapping
    @ApiOperation("批量删除分类")
    public Result delete(@RequestParam List<Long> ids){ //PathVariable 可以将前端的数据中带逗号分隔的id字符串转换成List<Long>类型
        log.info("批量删除分类：{}", ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> get(@PathVariable Long id){
        log.info("根据id查询菜品：{}", id);
        Dish dish = dishService.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorService.list(new QueryWrapper<DishFlavor>().eq("dish_id", id)));
        return Result.success(dishVO);
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateStatus(@PathVariable String status, @RequestParam Long id){
        log.info("修改菜品状态：{}，ids：{}", status, id);
        dishService.updateStatus(status, id);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(@RequestParam Long categoryId){
        log.info("根据分类id查询菜品：{}", categoryId);
        List<Dish> dishList = dishService.list(new QueryWrapper<Dish>().eq("category_id", categoryId));
        return Result.success(dishList);
    }

}
